/*------------------------------------------------------------------------------

	The MIT License
	
	Copyright (c) 2009 Adam Flater
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.

--------------------------------------------------------------------------------*/

package flater.utils
{

import mx.utils.StringUtil;


/**
 *  An extension of the <code>mx.utils.StringUtil</code> class. Provides additional 
 *  String utilities.
 * 
 *  <br/>                                                                                            
 *  Author: <a href="http://www.adamflater.net" target="_blank">
 *  Adam Flater</a>
 *  <br/>
 */
public class StringUtil extends mx.utils.StringUtil
{
    //--------------------------------------------------------------------------
    //
    //  Constants
    //
    //--------------------------------------------------------------------------

	public static const ELLIPSES	: String	= "...";
	public static const ONE_SPACE	: String	= " ";
	

    //--------------------------------------------------------------------------
    //
    //  Class methods
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  abbreviate
    //----------------------------------

    /**
     *  Abbreviates a String using ellipses.
     */		
	public static function abbreviate( string : String, maxWidth : int  ) : String
	{
		if ( maxWidth <= 0 )
		{
			return "";
		}
		else if ( maxWidth < 4 )
		{
			return ELLIPSES;
		}
		else if ( string.length < maxWidth )
		{
			return string;
		}
		else 
		{
			return string.substr( 0, maxWidth - 3 ) + ELLIPSES;
		}
	}


    //----------------------------------
    //  contains
    //----------------------------------

    /**
     *  Returns true if and only if this string contains the specified sequence of char values.
     */		
	public static function contains( string : String, sequence : String  ) : Boolean
	{
		if ( string == null )
		{
			return false;
		}
		
		if ( sequence == null )
		{
			return false;
		}
		
		return string.indexOf( sequence ) > -1;			
	}


    //----------------------------------
    //  endsWith
    //----------------------------------

    /**
     *  Tests if this string ends with the specified suffix - case sensitive.
     */		
	public static function endsWith( string : String, suffix : String  ) : Boolean
	{
		if ( string == null || suffix == null )
		{ 
			return false;
		}
		else if ( string.length < suffix.length )
		{
			return false;
		}
		else
		{
			return string.substr( string.length - suffix.length, suffix.length ) == suffix;
		}			
	}


    //----------------------------------
    //  endsWithIgnoreCase
    //----------------------------------

    /**
     *  Tests if this string ends with the specified suffix - case insensitive.
     */		
	public static function endsWithIgnoreCase( string : String, suffix : String  ) : Boolean
	{
		if ( string == null || suffix == null )
		{ 
			return false;
		}
		else
		{
			return endsWith( string.toLowerCase(), suffix.toLowerCase() );
		}
	}
	
	
    //----------------------------------
    //  lpad
    //----------------------------------

    /**
     *  Pads the left side of string by <code>length</code> characters using <code>char</code>.
     */		
	public static function lpad( string	: String, 
								 length	: int, 
								 char	: String = ONE_SPACE  ) : String
	{
		return pad( string, length, char );				
	}
	

    //----------------------------------
    //  pad
    //----------------------------------

    /**
     *  Pads a string by <code>length</code> characters using <code>char</code>.
     */		
	public static function pad( string : String, length : int, char : String = ONE_SPACE, 
    							 left : Boolean = true  ) : String
	{
		if ( string == null || length < 0 || char == null ) return null;
		 
		while( string.length < length )
		{
			if ( left )
			{
				string = char + string;
			}
			else
			{
				string = string + char;
			}
		}
		return string;				
	}
	
		
    //----------------------------------
    //  rpad
    //----------------------------------

    /**
     *  Pads the right side of string by <code>length</code> characters using <code>char</code>.
     */		
	public static function rpad( string	: String, 
								 length	: int, 
								 char	: String = ONE_SPACE ) : String
	{
		return pad( string, length, char, false);				
	}


    //----------------------------------
    //  startsWith
    //----------------------------------

    /**
     *  Tests if this string starts with the specified prefix - case sensitive.
     */		
	public static function startsWith( string : String, prefix : String  ) : Boolean
	{
		if ( string == null || prefix == null )
		{
			return false;
		}
		else if ( string.length < prefix.length )
		{ 
			return false;
		}
		else
		{
			return string.substr( 0, prefix.length ) == prefix;
		}			
	}


    //----------------------------------
    //  startsWithIgnoreCase
    //----------------------------------

    /**
     *  Tests if this string starts with the specified prefix - case insensitive.
     */		
	public static function startsWithIgnoreCase( string : String, prefix : String  ) : Boolean
	{
		if ( string == null || prefix == null )
		{ 
			return false;
		}
		else
		{
			return startsWith( string.toLowerCase(), prefix.toLowerCase() );
		}
	}


    //----------------------------------
    //  trim
    //----------------------------------

    /**
     *  Trims white space off of the beginning and end of a given String.
     */		
	public static function trim( string : String  ) : String
	{
		while( startsWith( string, " " ) )
		{
			string = string.substr( 1 );
		}
		while ( endsWith( string, " " ) )
		{
			string = string.substr( 0, string.length-1 );
		}
		
		return string;
	}

		
    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor.
     */
   	public function StringUtil()
	{
		super();
	}
	
			
} //  end class
} //  end package